[gcode_macro M303]                                                              # PID Autotune
gcode:
    {% if 'E' in params %}
        {% set heater = params.E|default(0)|int %}
        {% set temp = params.T|default(0)|float %}
        {% if heater == 0 %}                                                    # extruder
            {% if temp >= printer.configfile.settings.extruder.min_extrude_temp|float %}
                {% if temp <= printer.configfile.settings.extruder.max_temp|float %}
                    PID_CALIBRATE HEATER=extruder TARGET={T}
                {% endif %}
            {% endif %}
        {% elif heater == -1 %}                                                 # bed
            {% if temp <= printer.configfile.settings.heater_bed.max_temp|float %}
                PID_CALIBRATE HEATER=heater_bed TARGET={T}
            {% endif %}
        {% endif %}
    {% endif %}
